class Solution {//题目最关键的是去重
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
        int head=0,tail=nums.size()-1,sz=nums.size();
        sort(nums.begin(),nums.end());
        vector<vector<int>> ans;
        if(sz<3) return {};
        for(int i=0;i<sz-2;i++)
        {
            if(i>0&&nums[i]==nums[i-1]) continue;
            head=i+1;
            tail=sz-1;
            while(head<tail)
            {
                if(nums[head]+nums[tail]<-nums[i]) head++;
                else if(nums[head]+nums[tail]>-nums[i]) tail--;
                else 
                {
                    ans.push_back({nums[i],nums[head],nums[tail]});
                    while (head < tail && nums[head] == nums[head + 1]) ++head;
                    // 去重：移动 tail 避免重复值
                    

                    ++head;
                    --tail;
                }
            }
        }
        return ans;
    }
};
